import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_htmlful/flutter_htmlful.dart';

import 'search_cubit.dart';

class SearchPage extends StatefulWidget {
  const SearchPage({super.key});

  @override
  State<SearchPage> createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  String keywords = '';
  List list = ['aa'];

  showFilterDialog(BuildContext context, HomeIndexState state) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder:
          (context) => StatefulBuilder(
            builder:
                (context, setState) => SimpleDialog(
                  title: const Text("筛选"),
                  titlePadding: const EdgeInsets.all(15),
                  contentPadding: const EdgeInsets.all(15),
                  children: [
                    ...state.filterData
                        .map(
                          (element) => Div(
                            body: [
                              Text(element['name']),
                              Div(
                                body:
                                    (element['children'] as List)
                                        .map(
                                          (item) => RawChip(
                                            selected: item['isSelected'],
                                            selectedColor: Colors.blue[50],
                                            label: Text(item['label']),
                                            onSelected: (v) {
                                              debugPrint(v.toString());
                                              setState(() {
                                                item['isSelected'] = v;
                                              });
                                            },
                                          ),
                                        )
                                        .toList(),
                                style: Style(direction: Axis.horizontal, wrap: true, spacing: 10, runSpacing: 10, marginTop: 10, marginBottom: 10),
                              ),
                            ],
                          ),
                        )
                        .toList(),
                    Div(
                      body: [
                        ElevatedButton(
                          onPressed: () {
                            Navigator.pop(context);
                          },
                          child: const Text('确定'),
                        ),
                      ],
                      style: Style(direction: Axis.horizontal, alignment: WrapAlignment.end, wrap: true, spacing: 10, marginTop: 30),
                    ),
                  ],
                ),
          ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => HomeIndexCubit(),
      child: BlocBuilder<HomeIndexCubit, HomeIndexState>(
        builder: (BuildContext context, HomeIndexState state) {
          return Scaffold(
            appBar: AppBar(title: const Text('搜索'), backgroundColor: Colors.blue[100]),
            backgroundColor: Colors.white,
            body: Div(
              body: [
                Div(
                  body: [
                    IconButton(
                      onPressed: () {
                        showFilterDialog(context, state);
                      },
                      icon: const Icon(Icons.filter_alt_outlined),
                    ),
                    Div(
                      body: SearchBar(
                        leading: const Icon(Icons.search),
                        hintText: '输入关键字，空格分开',
                        trailing: <Widget>[IconButton(onPressed: () {}, icon: const Icon(Icons.credit_score_rounded))],
                        onSubmitted: (str) {
                          debugPrint(str);
                        },
                      ),
                      style: Style(flexGrow: 1),
                    ),
                  ],
                  style: [padding10, Style(direction: Axis.horizontal, crossAxisAlignment: CrossAxisAlignment.center)],
                ),
                Expanded(
                  child: ListView.builder(
                    itemCount: list.length,
                    itemBuilder: (context, index) {
                      return Text('data');
                    },
                  ),
                ),
              ],
              style: Style(),
            ),
          );
        },
      ),
    );
  }

  Style padding10 = Style(padding: 10);
}
